package Object.StudentManager;

import java.util.ArrayList;
import java.util.List;

public class StudentManager {
    private final List<Student> studentDataBase = new ArrayList<>();

    public Student getStudentById(String studentId) {
        for (Student student : studentDataBase) {
            if (student.getStudentId().equals(studentId)) {
                return student;
            }
        }
        return null;
    }

    public boolean addStudent(Student student) {
        if (student == null || getStudentById(student.getStudentId()) != null) {
            return false;
        }
        return studentDataBase.add(student);
    }

    public boolean deleteStudent(String studentId) {
        Student student = getStudentById(studentId);
        if (student != null) {
            return studentDataBase.remove(student);
        }
        return false;
    }

    public boolean updateStudent(String studentId, String name, int age) {
        Student student = getStudentById(studentId);
        if (student != null) {
            student.setName(name);
            student.setAge(age);
            return true;
        }
        return false;
    }

    public List<Student> getAllStudents() {
        return new ArrayList<>(studentDataBase);
    }
    // 添加这个方法
    public List<Student> searchStudents(String keyword) {
        List<Student> results = new ArrayList<>();
        for (Student student : studentDataBase) {
            if (student.getStudentId().contains(keyword) ||
                    student.getName().contains(keyword)) {
                results.add(student);
            }
        }
        return results;
    }
}

